#pragma once
#include<cstring>
#include<algorithm>
namespace My_Stl
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		typedef const T* const_iterator;

		vector()
			:_start(nullptr)
			,_finish(nullptr)
			,_endofstorage(nullptr)
		{}

		vector(size_t n,const T& val)
			:_start(new T[n])
			,_finish(_start+n)
			,_endofstorage(_finish)
		{
			for (size_t i = 0; i < n; i++)
			{
				_start[i] = val;
			}
		}

		vector(const vector<T>& x)
			:_start(new T[x.size()])
			,_finish(_start + x.size())
			,_endofstorage(_finish)
		{
			for (size_t i=0;i < x.size();i++)
			{
				_start[i] = x._start[i];
			}
		}

		vector& operator= (vector<T> x)
		{
			swap(x);
			return *this;
		}

		~vector()
		{
			delete[] _start;
			_start = _finish = _endofstorage = nullptr;
		}

		iterator begin()
		{
			return _start;
		}

		const_iterator begin()const
		{
			return _start;
		}

		iterator end()
		{
			return _finish;
		}

		const_iterator end()const
		{
			return _finish;
		}

		size_t size()const
		{
			return (size_t)(_finish - _start);
		}

		size_t capacity()const
		{
			return _endofstorage - _start;
		}

		bool empty()const
		{
			return !(size());
		}

		T& operator[](size_t n)
		{
			return *(_start + n);
		}

		const T& operator[](size_t n)const
		{
			return *(_start + n);
		}

		void swap(vector<T>& x)
		{
			std::swap(_start, x._start);
			std::swap(_finish, x._finish);
			std::swap(_endofstorage, x._endofstorage);
		}

		void reserve(size_t n)
		{
			size_t sz = size();
			if (n > capacity())
			{
				T* tmp = new T[n];
				for (size_t i = 0; i < sz; i++)
				{
					tmp[i] = _start[i];
				}

				delete[] _start;
				_start = tmp;
				tmp = nullptr;
				_finish = _start + sz;
				_endofstorage = _start + n;
			}
		}

		void resize(size_t n, T val = T())
		{
			if (n < size())
			{
				_finish = _start + n;
			}
			else
			{
				reserve(n); 
				size_t old_size = size();
				for (size_t i = old_size; i < n; i++)
				{
					*(_finish++) = val; 
				}
			}
		}

		void push_back(const T& val)
		{
			if (_finish == _endofstorage)
			{
				if (capacity() == 0)
				{
					_start = _finish = new T[4];
					_endofstorage = _start + 4;
				}
				else
				{
					reserve(2 * capacity());
				}
			}
			*_finish = val;
			_finish++;
		}

		void pop_back()
		{
			if (size())
			{
				_finish--;
			}
		}

		iterator insert(iterator position, const T& val)
		{
			if (_finish == _endofstorage)
			{
				size_t pos = position - _start;
				reserve(capacity() == 0 ? 4 : 2 * capacity());
				position = _start + pos;
			}
			iterator it = _finish-1;
			while (it >= position)
			{
				*(it+1) = *it;
				--it;
			}
			*position = val;
			_finish++;
			return position;
		}

		iterator insert(iterator position, int n, const int& val)
		{

			if (size() + n > capacity())
			{
				size_t pos = position - _start;
				reserve(size() + n);
				position = _start + pos;
			}
			iterator it = _finish - 1;
			while (it >= position)
			{
				*(it + n) = *it;
				--it;
			}
			for (size_t i = 0; i < n; i++)
			{
				*(position + i) = val;
			}
			_finish += n;
			return position;
		}

		iterator insert(iterator position, size_t n, const T& val)
		{

			if (size() + n > capacity())
			{
				size_t pos = position - _start;
				reserve(size()+n);
				position = _start + pos;
			}
			iterator it = _finish - 1;
			while (it >= position)
			{
				*(it+n) = *it;
				--it;
			}
			for (size_t i = 0; i < n; i++)
			{
				*(position + i) = val;
			}
			_finish += n;
			return position;
		}

		template <class InputIterator>
		iterator insert(iterator position, InputIterator first, InputIterator last)
		{
			size_t n = 0;
			InputIterator tmp_first = first;
			while (tmp_first != last)
			{
				++tmp_first;
				++n;
			}

			if (size() + n > capacity())
			{
				size_t pos = position - _start;
				reserve(size() + n);
				position = _start + pos;
			}

			iterator it = _finish - 1;
			while (it >= position)
			{
				*(it + n) = *it;
				--it;
			}

			iterator tmp_position = position;
			while (first != last)
			{
				*tmp_position = *first;
				++tmp_position;
				++first;
			}
			_finish += n;
			return position;
		}

		iterator erase(iterator position)
		{
			iterator it = position+1;
			while (it != _finish)
			{
				*(it-1) = *it;
				++it;
			}
			--_finish;
			return position;
		}

		iterator erase(iterator first, iterator last)
		{
			size_t n = last - first;
			iterator it = first;
			while (it <= last)
			{
				*it = *(it+n);
				++it;
			}
			_finish -= n;
			return first;
		}

	private:
		iterator _start;
		iterator _finish;
		iterator _endofstorage;
	};
}